//
//  SmallDecisionsSzSetViewController.m
//  SmallDecisions
//
//  Created by 李付雄 on 2025/8/12.
//

#import "SmallDecisionsSzSetViewController.h"
#import "SmallDecisionsSzViewController.h"
@interface SmallDecisionsSzSetViewController ()
@property(nonatomic,strong)UIView *containerView;
@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UILabel *contentLabel;
@property(nonatomic,strong)UIButton *sureBtn;
@property (nonatomic, strong) UISlider *slider;
@end

@implementation SmallDecisionsSzSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"骰子";
    [self containerView];
    [self titleLabel];
    [self contentLabel];
    [self slider];
    [self sureBtn];
}

- (void)sliderValueChange:(UISlider *)slider {
    self.contentLabel.text = [NSString stringWithFormat:@"%.0f",roundf(self.slider.value)];
}

- (UIView *)containerView {
    if(!_containerView) {
        _containerView = [[UIView alloc] init];
        _containerView.backgroundColor = SmallDecisionsPurpleColor;
        _containerView.layer.cornerRadius = 10;
        _containerView.layer.masksToBounds = YES;
        [self.view addSubview:_containerView];
        [_containerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(30);
            make.right.mas_equalTo(-30);
            make.center.mas_equalTo(0);
        }];
    }
    return _containerView;
}

- (UILabel *)titleLabel {
    if(!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = SmallDecisionsYellowColor;
        _titleLabel.text = @"骰子个数";
        _titleLabel.font = SmallDecisionsSetBoldFontSize(22);
        _titleLabel.textAlignment = 1;
        [self.containerView addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(20);
            make.left.mas_equalTo(20);
            make.right.mas_equalTo(-20);
        }];
    }
    return _titleLabel;
}

- (UILabel *)contentLabel {
    if(!_contentLabel) {
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.textColor = SmallDecisionsThemeFontColor;
        _contentLabel.font = SmallDecisionsSetBoldFontSize(50);
        _contentLabel.numberOfLines = 0;
        _contentLabel.textAlignment = 1;
        _contentLabel.text = @"1";
        [self.containerView addSubview:_contentLabel];
        [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.titleLabel.mas_bottom).mas_offset(30);
            make.left.mas_equalTo(20);
            make.right.mas_equalTo(-20);
        }];
    }
    return _contentLabel;
}

- (UISlider *)slider {
    if(!_slider) {
        _slider = [[UISlider alloc] init];
        _slider.minimumTrackTintColor = SmallDecisionsThemeColor;
        [_slider setThumbImage:[UIImage imageNamed:@"slider_thumb_img"] forState:UIControlStateNormal];
        [_slider setThumbImage:[UIImage imageNamed:@"slider_thumb_img"] forState:UIControlStateFocused];
        [_slider addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventValueChanged];
        _slider.continuous = YES;
        _slider.minimumValue = 1;
        _slider.maximumValue = 6;
        _slider.value = 1;
        [self.containerView addSubview:_slider];
        [_slider mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.contentLabel.mas_bottom).offset(30);
            make.left.mas_equalTo(20);
            make.height.mas_equalTo(50);
            make.right.mas_equalTo(-20);
        }];
    }

    return _slider;
}

- (UIButton *)sureBtn {
    if(!_sureBtn) {
        _sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sureBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_sureBtn setTitleColor:SmallDecisionsThemeFontColor forState:UIControlStateNormal];
        [_sureBtn setTitleColor:[UIColor clearColor] forState:UIControlStateHighlighted];
        _sureBtn.titleLabel.font = SmallDecisionsSetBoldFontSize(18);
        _sureBtn.layer.cornerRadius = 25;
        _sureBtn.layer.masksToBounds = YES;
        _sureBtn.backgroundColor = SmallDecisionsThemeColor;
        [_sureBtn addTarget:self action:@selector(sureBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [self.containerView addSubview:_sureBtn];
        [_sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(50);
            make.left.right.equalTo(self.slider);
            make.top.equalTo(self.slider.mas_bottom).offset(50);
            make.bottom.mas_offset(-30);
        }];
    }
    return _sureBtn;
}

- (void)sureBtnAction {
    if([SmallDecisionsSingletonHelper shareInstance].isVIP) {
        [self pushSzViewController];
    } else {
        NSUInteger number = [SmallDecisionsSingletonHelper getUserNumberWithKey:@"SXUSERNUMBER"];
        if(number > 3) {
            [SmallDecisionsSingletonHelper openVipViewController];
        } else {
            [SmallDecisionsSingletonHelper setUserNumberWithKey:@"SXUSERNUMBER" number:number + 1];
            [self pushSzViewController];
        }
    }
    
}

- (void)pushSzViewController {
    SmallDecisionsSzViewController *szVC = [[SmallDecisionsSzViewController alloc] init];
    szVC.diceCount = roundf(self.slider.value);
    [SmallDecisionsNavigatorManager pushViewController:szVC];
}

@end
